package Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * CaptiansLog class holds data in an ArrayList pertaining to events 
 * that happen in the game, such as market transactions and location changes.
 * This data will be used and displayed by the GUI to let the player 
 * see past events that have occurred.
 * 
 * @author Tim Kerr
 * @version 1
 * 
 *
 */
public class CaptainsLog implements Serializable{
	/**
	 * List of entries
	 */
	private List<String> entries;//entries list
	
	/**
	 * Integer counter
	 */
	private int counter;//keeps track of number of entries
	
	/**
	 * Capacity of the entries list before it loop back around
	 */
	private final int cap = 10; // $codepro.audit.disable numericLiterals
	
	/**
	 * Constructor
	 */
	public CaptainsLog(){
		entries = new ArrayList<String>();
		counter = 0;
	}
	
	/**
	 * Adds 'str' to the ArrayList to be displayed by the GUI. Holds at most 50 entries. 
	 * After 50 entries have been added,
	 * older entries will be overwritten.
	 * @param str could be String pertaining to an Event, market transaction, 
	 * or player location change
	 */
	public void publish(String str){
		if(entries.size() < cap){	
			entries.add(str);
			System.out.println("Added to log. " + entries.size());
		}
				
		else{//10 cap has been reached. Begin overwriting data at the beginning of the array.			
			System.out.println("Looping back around now. Counter = " + counter);
			if(counter < cap){
				entries.remove(counter);
				entries.add(counter, str);
				counter++;	
			}
			else{//loop back around once the end has been reached
				counter = 0;
				entries.remove(counter);
				entries.add(counter, str);
				counter++;
			}
		}	
	}
	
	/**
	 * Returns inner ArrayList containing all log entries
	 * @return List of entries
	 */
	public List<String> getEntries(){
		return entries;
	}
	
	/**
	 * toString method
	 * @return String
	 */
	public String toString(){
		String str = "";
		for(int i = 0; i < entries.size(); i++){
			str += entries.get(i) + "\n";
		}
				
		return str;
	}
	
	/**
	 * Clears the inner ArrayList of all its entries.
	 */
	public void clearList(){
		entries.clear();
		counter = 0;
	}
}
